<template>
  <div>
    <div class="play" v-if="musicData.songinfo">
      <div class="header">
        <div class="title">
          <router-link to="/">
            <i class="iconfont icon-shouye left"></i>
          </router-link>
          <div class="music-info">
            <p>{{musicData.songinfo.title}}</p>
            <p class="author">{{musicData.songinfo.author}}</p>
          </div>
          <router-link to="/search">
            <i class="iconfont icon-sousuo right"></i>
          </router-link>
        </div>
      </div>
      <div class="cong-info">
        <div class="song-info-img">
<!--          currentTimeData:0,-->
<!--          durationTime:0-->
          <img :src="musicData.songinfo.pic_huge" :alt="musicData.songinfo.pic_huge">
          <Lrc :musicSongid="songid" :currentTimeData="currentTimeData" :durationTime="durationTime"/>
        </div>
        <div class="iconbox">
          <i class="iconfont icon-shoucang2 left"></i>
          <i class="box"></i>
          <i class="iconfont icon-xiazai right"></i>
        </div>
      </div>
      <div class="song">
        <audio ref="player" :src="musicData.bitrate.show_link" controls></audio>
      </div>
    </div>
  </div>
</template>

<script>
  import api from "../../api/base";
  import Lrc from "./Lrc";

    export default {
        name: "Player",
        data(){
          return{
            musicData:{},
            currentTimeData:0,
            durationTime:0
          }
        },
      components:{
        Lrc
      },
      mounted(){
        // console.log(this.songid);
        api.getMusicList({
          method:'baidu.ting.song.play',
          songid: this.songid
        })
        .then(res=>{
          this.musicData = res.data;
          this.$nextTick(()=>{
            this.addEventHandle();
          })
          // console.log(res.data);
        })

      },
      beforeDestroy() {
          this.removeEventListHandle();
      },
      methods:{
        addEventHandle(){
          this.$refs.player.addEventListener('timeupdate',this.addCurrentTime);
          this.$refs.player.addEventListener('canplay',this.addDuration);
        },
        removeEventListHandle(){
          this.$refs.player.removeEventListener('timeupdate',this.addCurrentTime);
          this.$refs.player.removeEventListener('canplay',this.addDuration);
        },
        addCurrentTime(){
          // console.log(this.$refs.player.currentTime);
          this.currentTimeData = this.$refs.player.currentTime;
        },
        addDuration(){
          // console.log(this.$refs.player.duration);
          this.durationTime = this.$refs.player.duration;
        }
        /**
         * currentTime:当前正在播放的时间
         * duration:播放总时间
         */
      } ,
      props:{
          songid:{
            type:[String,Number],
            interval:true
          }
      }
    }
</script>

<style scoped>
  .header {
    padding: 15px;
  }

  .music-info {
    flex: 1;
    font-size: 20px;
  }

  .title {
    display: flex;
    text-align: center;
  }

  .left {
    font-size: 30px;
  }

  .ca {
    color: red;
  }

  .right {
    font-size: 30px;
  }

  .song-info {
    padding: 15px;
  }

  .song-info-img {
    text-align: center;
  }

  .song-info-img img {
    width: 50%;
    border-radius: 5px;
    box-shadow: 0 0 10px 0 rgba(50, 50, 50, 0.31);
  }

  .song-lrc {
    margin-top: 10px;
    min-height: 50px;
  }

  .iconbox {
    display: flex;
    margin-top: 30px;
  }

  .iconbox .box {
    flex: 1;
  }

  .song {
    width: 100%;
    text-align: center;
  }

  .song audio {
    width: 80%;
  }

  .active {
    color: #222;
  }

  .author {
    font-size: 12px;
    color: #999;
  }


</style>
